package tk.kolesnikov.hashes;

import tk.kolesnikov.algorithms.DCTHelper;
import tk.kolesnikov.algorithms.ImageUtils;
import tk.kolesnikov.algorithms.MatrixHelper;

import java.awt.image.BufferedImage;

/**
 * Created by seko0313 on 15.01.14.
 */
public class PHash implements ImageHash {

    public String getHash(BufferedImage image)  {
        ImageUtils imageUtils = new ImageUtils();
        image = imageUtils.resize(image, 32, 32);
        image = imageUtils.graysсale(image);
        double[][] imageMatrix = imageUtils.grayscaleImageToMatrix(image);
        double[][] dctResult = DCTHelper.dct2(imageMatrix, 0);
        double[][] reducedDCTResult = MatrixHelper.reduceMatrix(dctResult, 8);
        double average = MatrixHelper.getAverage(reducedDCTResult);
        int[][] mask = MatrixHelper.getAverageMask(reducedDCTResult, average);
        return imageUtils.averageMaskToHash(mask);
    }

}
